import QtQuick 1.1
import com.nokia.symbian 1.1
import com.nokia.extras 1.1

import "logicaObject.js" as LogicaObject

Page {
    id: root
    tools: ToolBarLayout {
        id: toolBarLayout
        enabled: !root.pageStack.busy
        ToolButton {
            flat: true
            iconSource: "toolbar-back"
            onClicked: window.pageStack.depth <= 1 ? Qt.quit() : window.pageStack.pop()
        }

        ToolButton {
            iconSource: "qrc:///toolbar_ajuda"
            onClicked: {
                root.pageStack.push(Qt.resolvedUrl("Ajuda.qml"));
            }
        }

        ToolButton {
            iconSource: "qrc:///toolbar_info"
            onClicked: {
                root.pageStack.push(Qt.resolvedUrl("Sobre.qml"));
            }
        }

        ToolButton {
            flat: true
            iconSource: "toolbar-menu"
            onClicked: {                
                menu.open();
            }
        }
    }

    property bool apresentacaoSomente : true

    Menu {
        id: menu
        MenuLayout {
            MenuItem {
                text: qsTr("_menu_novo_jogo_")
                onClicked: {
                    root.apresentacaoSomente = false;
                    imgDone.visible = false;
                    imgMovimento.up();                    
                    LogicaObject.instancias[root.toString()].newGame();
                    root.carregarTela(true);
                }
            }
            MenuItem {
                text: qsTr("_menu_tela_inicial_")
                visible: !root.apresentacaoSomente
                onClicked: {
                    root.apresentacaoSomente = true;
                    imgDone.visible = false;
                    LogicaObject.instancias[root.toString()].reset();
                    root.carregarTela();
                }
            }
        }
    }

    Timer {
        running: root.apresentacaoSomente && Qt.application.active && root.status == PageStatus.Active
        interval: 800
        repeat: true
        onTriggered: {
            var jogada = LogicaObject.instancias[root.toString()].jogadaAleatoria();
            grdTabuleiro.mover(null, jogada[0]);
        }
    }

    Component.onCompleted: {
        var objLogica = LogicaObject.createObject(root.toString(), grdTabuleiro.columns);
        grdTabuleiro.desenhar();
        dlgFimJogo.open();
    }

    InfoBanner {
        id: info
        timeout: 1000        
    }

    MeuGrid {
        id: grdTabuleiro
        keyObject: root.toString()
        info: info
        dlgFim: dlgFimJogo
        enabled: !root.apresentacaoSomente && !imgDone.visible
        anchors.centerIn: parent

        onMoveComplete: {
            root.carregarTela(false);
        }
    }

    function carregarTela(carregaGrid) {
        var objLogica = LogicaObject.instancias[root.toString()];
        if (carregaGrid)
            grdTabuleiro.carregarGrid();
        imgMovimento.movimentar();
        txtMovimentos.text = objLogica.movimentos;
    }

    Rectangle {
        id: rectTabuleiro
        color: "#7CB5E8"
        width: grdTabuleiro.width + 30; height: grdTabuleiro.height + 30
        anchors.centerIn: parent

        Rectangle {
            color: platformStyle.colorNormalDark
            width: parent.width - 30; height: parent.height - 30
            anchors.centerIn: parent
        }
    }

    Grid {
        anchors { top: rectTabuleiro.bottom; topMargin: 10 }
        columns: 2
        anchors.horizontalCenter: parent.horizontalCenter
        visible: !root.apresentacaoSomente

        Row {
            spacing: 5            
            Image {
                id: imgMovimento
                source: "qrc:///movup"
                width: 25; height: 40

                property bool isUp : true

                function movimentar() {
                    if (imgMovimento.isUp)
                        imgMovimento.down();
                    else
                        imgMovimento.up();
                }

                function up() {
                    imgMovimento.source = "qrc:///movup"
                    imgMovimento.isUp = true
                }

                function down() {
                    imgMovimento.source = "qrc:///movdown"
                    imgMovimento.isUp = false
                }
            }

            Text {
                id: txtMovimentos
                text: (LogicaObject.instancias[root.toString()] ? LogicaObject.instancias[root.toString()].movimentos : 0)
                width: 40
                font.bold: true
                font.family: "Letter Gothic Std"
                color: platformStyle.colorNormalLight
            }
        }
    }

    Dialog {
        id: dlgFimJogo
        content: Rectangle {
            id: rect
            width: parent.width; height: contentCol.height
            color: "white"
            Column {
                id: contentCol
                anchors.centerIn: parent
                spacing: 20
                Image {
                    id: joia
                    source: "qrc:///good"
                }

                Text {
                    text: qsTr("_fim_jogo_")
                    font.pixelSize: 25
                    horizontalAlignment: Text.AlignHCenter
                }
            }

            SequentialAnimation {
                id: animJoia
                running: Qt.application.active && dlgFimJogo.visible
                loops: Animation.Infinite
                NumberAnimation { target: joia; property: "x"; to: rect.width - joia.width; duration: 500 }
                NumberAnimation { target: joia; property: "x"; to: 0; duration: 500 }
            }
        }
        buttons: Button {
                text: "Ok"
                anchors.centerIn: parent
                onClicked: {
                    dlgFimJogo.close();
                    imgDone.visible = true;
                }
            }
        onStatusChanged: {
            if (status === DialogStatus.Open) {
                animJoia.restart();
            }
        }
    }

    Image {
        id: imgDone
        source: "qrc:///" + (local === "brasil" ? "done" : "done_eng")
        anchors.centerIn: parent
        visible: false
        z: grdTabuleiro.z + 1

        onVisibleChanged: {
            if (imgDone.visible) {
                animImgDone.restart();
            }
        }

        SequentialAnimation {
            id: animImgDone
            running: imgDone.visible
            NumberAnimation {
                target: imgDone; property: "opacity"; from: 1; to: 0.4; duration: 1000;
            }
        }
    }
}
